<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>部门管理</el-breadcrumb-item>
      <el-breadcrumb-item>职位列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--    查询职位-->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="职位级别名称" clearable v-model="pname" @clear="getPositionList">
          <el-button slot="append" icon="el-icon-search" @click="getPositionList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="addDialogVisible=true">创建</el-button>
      </el-col>
    </el-row>
    <!--    添加职位-->
    <el-dialog title="添加职位" :visible.sync="addDialogVisible" width="50%" :close-on-click-modal="false"
               @close="addPositionFormClose">
      <el-form
          :model="addPositionForm"
          :rules="addPositionFormRules"
          ref="addPositionFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="级别编码" prop="pid">
          <el-input v-model="addPositionForm.pid"></el-input>
        </el-form-item>
        <el-form-item label="职位级别名称" prop="pname">
          <el-input v-model="addPositionForm.pname"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="addPositionForm.level"></el-input>
        </el-form-item>

        <el-form-item label="上级职位">
          <el-select v-model="addPositionForm.parentId" placeholder="上级职位" prop="parentId">
            <el-option :label="item.pname" :value="item.id" v-for="item in positionList1" :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="部门">
          <el-select v-model="addPositionForm.did" placeholder="部门" prop="did">
            <el-option :label="item.dname" :value="item.id" v-for="item in deptList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addPosition">确 定</el-button>
        <el-button @click="cancelAddPosition">取 消</el-button>
      </span>
    </el-dialog>

    <!--    主页渲染数据-->
    <el-table
        :data="positionList"
        stripe
        style="width: 100%">
      <el-table-column
          prop="id"
          label="编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="pid"
          label="级别编码"
          width="180">
      </el-table-column>
      <el-table-column
          prop="pname"
          label="职位级别名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="level"
          label="级别">
      </el-table-column>
      <el-table-column
          prop="parentName"
          label="上级组织">
      </el-table-column>
      <el-table-column
          prop="deptName"
          label="部门">
      </el-table-column>
      <el-table-column fixed="right" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="removePositionById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6, 9, 12]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <!--    修改-->
    <el-dialog
        title="修改职位"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editPositionFormClose"
        :close-on-click-modal="false"
    >
      <el-form
          :model="editPositionForm"
          :rules="editPositionFormRules"
          ref="editPositionFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="级别编码" prop="pid">
          <el-input v-model="editPositionForm.pid"></el-input>
        </el-form-item>
        <el-form-item label="职位级别名称" prop="pname">
          <el-input v-model="editPositionForm.pname"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="editPositionForm.level"></el-input>
        </el-form-item>

        <el-form-item label="上级职位">
          <el-select v-model="editPositionForm.parentId" placeholder="上级职位" prop="parentId">
            <el-option :label="item.pname" :value="item.id" v-for="item in positionList1" :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="部门">
          <el-select v-model="editPositionForm.did" placeholder="部门" prop="did">
            <el-option :label="item.dname" :value="item.id" v-for="item in deptList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editPosition">确 定</el-button>
        <el-button @click="editDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
import manager from "@/components/system/Manager";

export default {
  data() {
    return {
      positionList: [],
      deptList: [],
      positionList1: [],
      pageSize: 3,
      pageNum: 1,
      total: 2,
      addDialogVisible: false,
      editDialogVisible: false,
      positionName: "",
      parentId: "",
      deptId: "",
      pname: "",
      addPositionForm: {
        pid: "",
        pname: "",
        level: "",
        parentId: "",
        did: "",
      },
      addPositionFormRules: {},
      editPositionForm: {
        pid: "",
        pname: "",
        level: "",
        parentName: "",
        deptName: "",
      },
      editPositionFormRules: {},

    }
  },
  methods: {
    // 查询所有职位
    getPositionList() {
      let url = this.$HRMSUrl + "/position/listAllPosition"
      this.$axios.get(url, {
        params: {
          name: this.pname,
          currentPage: this.pageNum,
          pageSize: this.pageSize,
          total: this.total
        }
      })
          .then(res => {
            if (res.data.code == 200) {
              this.positionList = res.data.data.list;
              this.pageNum = res.data.data.pageNum;
              this.pageSize = res.data.data.pageSize;
              this.total = res.data.data.total;

            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
                duration: 1000
              })
            }
          })
    },
    //添加职位
    addPosition() {
      this.$refs.addPositionFormRef.validate(valid => {
        if (valid) {
          let url = this.$HRMSUrl + "/position/addPosition";
          this.$axios.post(url, this.addPositionForm)
              .then(res => {
                if (res.data.code === 200) {
                  this.addDialogVisible = false;
                  this.initPosition();

                } else {
                  this.$message({
                    type: "error",
                    message: res.data.msg,
                    duration: 1000
                  })
                }
              })
        } else {
          return false;
        }
      })
    },

    //取消添加
    cancelAddPosition() {
      this.addDialogVisible = false;
      this.addPositionForm = {};
    },

    //关闭添加对话框
    addPositionFormClose() {
      this.$refs.addPositionFormRef.resetFields();

    },

    //显示修改框
    showEditDialog(row) {
      this.editDialogVisible = true;
      this.editPositionForm = row;
    },

    //关闭编辑对话框
    editPositionFormClose() {
      this.$refs.editPositionFormRef.resetFields();
    },

    //修改职位
    editPosition() {
      let url = this.$HRMSUrl + "position/editPosition";
      this.$axios.put(url, this.editPositionForm)
          .then(res => {
            if (res.data.code === 200) {
              this.editDialogVisible = false;
              this.getPositionList();
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
                duration: 1000
              })
            }
          })
    },

    //删除职位
    removePositionById(id) {
      let url = this.$HRMSUrl + "/position/delete/" + id;
      this.$confirm('此数据将将永久删除该数据，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(url).then(res => {
          if (res.data.code === 200) {
            this.getPositionList();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
              duration: 1000
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        })
      })


    },
    initDept() {
      let url = this.$HRMSUrl + "dept/listAllDept";
      this.$axios.get(url).then(res => {
        if (res.data.code === 200) {
          this.deptList = res.data.data.list;
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
            duration: 1000
          })
        }
      })
    },
    initPosition() {
      let url = this.$HRMSUrl + "position/initPosition";
      this.$axios.get(url).then(res => {
        if (res.data.code === 200) {
          this.positionList1 = res.data.data;
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
            duration: 1000
          })
        }
      })
    },


    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getPositionList();
    },

    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.getPositionList();
    },

    loadMenus() {
      this.getPositionList();
    }
  },

  created() {
    this.loadMenus();
    this.initDept();
    this.initPosition();
  }
}
</script>